home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / SDKs / Apple Game Sprockets / InputSprocket / Sample Drivers / Common Driver Code / String_Utils.cp < prev    next >
Encoding:
Text File  |  1998-07-14  |  5.7 KB  |  198 lines  |  [TEXT/CWIE]

  1. // ===========================================================================
  2. //    String_Utils.cp                     ©1993 Metrowerks Inc. All rights reserved.
  3. // ===========================================================================
  4. //
  5. //    String manipulation utility functions
  6.  
  7. #ifndef __MEMORY__
  8. #include <Memory.h>
  9. #endif
  10.  
  11. #ifndef __SCRIPT__
  12. #include <Script.h>
  13. #endif
  14.  
  15. #include "String_Utils.h"
  16. #include "Common.h"
  17.  
  18.  
  19. // ---------------------------------------------------------------------------
  20. //        • CopyPStr
  21. // ---------------------------------------------------------------------------
  22. //    Copy a Pascal string
  23. //
  24. //        Returns a pointer to the copied string
  25. //
  26. //        inDestSize specifies the maximum size of the destination (INCLUDING
  27. //            the length byte). The default is sizeof Str255.
  28. //
  29. //        By calling this function as:
  30. //
  31. //            CopyPStr(source, dest, sizeof(dest));
  32. //
  33. //        you can change the declaration of dest without having to
  34. //        remember to change the call.
  35.  
  36. StringPtr
  37. CopyPStr(
  38.     ConstStr255Param    inSourceStr,
  39.     StringPtr            outDestStr,
  40.     SInt16                inDestSize)
  41. {
  42. #if DEBUG
  43.     UInt32 dbgSrcFirst      = (UInt32) inSourceStr;
  44.     UInt32 dbgSrcLast       = dbgSrcFirst + inSourceStr[0];
  45.     UInt32 dbgDestFirst     = (UInt32) outDestStr;
  46.     UInt32 dbgDestLast      = dbgDestFirst + inDestSize - 1;
  47. #endif
  48.  
  49.     WARNING(inSourceStr != nil,        "CopyPStr inSourceStr == nil");
  50.     WARNING(outDestStr != nil,        "CopyPStr outDestStr == nil");
  51.     WARNING(inDestSize > 0,            "CopyPStr outDestStr <= 0");
  52.     WARNING((dbgSrcFirst > dbgDestLast) || (dbgSrcLast < dbgDestFirst),
  53.                                     "CopyPStr strings overlapped");
  54.  
  55.     SInt16    dataLen = inSourceStr[0] + 1;
  56.     if (dataLen > inDestSize) {
  57.         dataLen = inDestSize;
  58.     }
  59.     
  60.     ::BlockMoveData(inSourceStr, outDestStr, dataLen);
  61.     outDestStr[0] = dataLen - 1;
  62.     return outDestStr;
  63. }
  64.  
  65. // ---------------------------------------------------------------------------
  66. //        • ConcatPStr
  67. // ---------------------------------------------------------------------------
  68. //    Concatenate two Pascal strings. The first string becomes the combination
  69. //    of the first and second strings.
  70. //
  71. //        Returns a pointer to the concatenated string
  72. //
  73. //        inDestSize specifies the maximum size of the concatenated string
  74. //            (INCLUDING the length byte). The default is sizeof Str255.
  75. //
  76. //        By calling this function as:
  77. //
  78. //            ConcatPStr(io, appendMe, sizeof(io));
  79. //
  80. //        you can change the declaration of io without having to
  81. //        remember to change the call.
  82.  
  83. StringPtr
  84. ConcatPStr(
  85.     Str255                ioFirstStr,
  86.     ConstStr255Param    inSecondStr,
  87.     SInt16                inDestSize)
  88. {
  89. #if DEBUG
  90.     UInt32 dbgSrcFirst      = (UInt32) ioFirstStr;
  91.     UInt32 dbgSrcLast       = dbgSrcFirst + inDestSize - 1;
  92.     UInt32 dbgDestFirst     = (UInt32) inSecondStr;
  93.     UInt32 dbgDestLast      = dbgDestFirst + inSecondStr[0];
  94. #endif
  95.  
  96.     WARNING(ioFirstStr != nil,    "ConcatPStr ioFirstStr == nil");
  97.     WARNING(inSecondStr != nil,    "ConcatPStr inSecondStr == nil");
  98.     WARNING(inDestSize > 0,        "ConcatPStr inDestSize <= 0");
  99.     WARNING((dbgSrcFirst > dbgDestLast) || (dbgSrcLast < dbgDestFirst),
  100.                                 "ConcatPStr strings overlapped");
  101.  
  102.                                 // Limit combined string to inDestSize chars
  103.     SInt16    charsToCopy = inSecondStr[0];
  104.     if (ioFirstStr[0] + charsToCopy > inDestSize - 1) {
  105.         charsToCopy = inDestSize - 1 - ioFirstStr[0];
  106.     }
  107.  
  108.                                 // Copy second to end of first string
  109.     ::BlockMoveData(inSecondStr + 1,  ioFirstStr + ioFirstStr[0] + 1,
  110.                         charsToCopy);
  111.                                 // Set length of combined string
  112.     ioFirstStr[0] += charsToCopy;
  113.     
  114.     return ioFirstStr;
  115. }
  116.  
  117.  
  118. Boolean        PStr_Identical(ConstStr255Param inStringOne, ConstStr255Param inStringTwo)
  119. {
  120.     WARNING(inStringOne != nil,        "PStr_Identical inStringOne == nil");
  121.     WARNING(inStringTwo != nil,        "PStr_Identical inStringTwo == nil");
  122.  
  123.     UInt32 itr;
  124.  
  125.     for(itr = 0; itr <= inStringOne[0]; itr++)
  126.     {
  127.         // found a difference return false
  128.         if (inStringOne[itr] != inStringTwo[itr])
  129.             { return false; }
  130.     }
  131.     
  132.     return true;
  133. }
  134.  
  135. #if 0
  136. // ---------------------------------------------------------------------------
  137. //        • CopyPStrIntl
  138. // ---------------------------------------------------------------------------
  139. //    Copy a Pascal string
  140. //
  141. //        Returns a pointer to the copied string
  142. //
  143. //        inDestSize specifies the maximum size of the destination (INCLUDING
  144. //            the length byte). The default is sizeof Str255.
  145. //
  146. //        By calling this function as:
  147. //
  148. //            CopyPStr(source, dest, sizeof(dest));
  149. //
  150. //        you can change the declaration of dest without having to
  151. //        remember to change the call.
  152. StringPtr    CopyPStrIntl(ConstStr255Param inSourceStr, StringPtr outDestStr,
  153.                             SInt16 inDestSize)
  154. {
  155.     WARNING(0,                        "CopyPStrIntl has not been debugged yet do not use w/o doing so");
  156.  
  157. #if DEBUG
  158.     UInt32 dbgSrcFirst      = (UInt32) inSourceStr;
  159.     UInt32 dbgSrcLast       = dbgSrcFirst + inSourceStr[0];
  160.     UInt32 dbgDestFirst     = (UInt32) outDestStr;
  161.     UInt32 dbgDestLast      = dbgDestFirst + inDestSize - 1;
  162. #endif
  163.  
  164.     WARNING(inSourceStr != nil,        "CopyPStr inSourceStr == nil");
  165.     WARNING(outDestStr != nil,        "CopyPStr outDestStr == nil");
  166.     WARNING(inDestSize > 0,            "CopyPStr outDestStr <= 0");
  167.     WARNING((dbgSrcFirst > dbgDestLast) || (dbgSrcLast < dbgDestFirst),
  168.                                     "CopyPStr strings overlapped");
  169.  
  170.     short   charType;
  171.     SInt16  charsToCopy = inDestSize - 1;
  172.  
  173.     if ( inSourceStr[0] > charsToCopy )
  174.     {
  175.         /* Make sure the string isn't truncated in the middle of */
  176.         /* a multi-byte character. */
  177.         while (charsToCopy != 0)
  178.         {
  179.             charType = CharByte((Ptr)&inSourceStr[1], charsToCopy);
  180.             if ((charType == smSingleByte) || (charType == smLastByte))
  181.                 break;  /* inSourceStr[charsToCopy] is now a valid last character */
  182.             --charsToCopy;
  183.         }
  184.     }
  185.     else
  186.     {
  187.         charsToCopy = inSourceStr[0];
  188.     }
  189.  
  190.     /* Set the outDestStr string length */
  191.     outDestStr[0] = charsToCopy;
  192.     /* and copy charsToCopy characters */
  193.     ::BlockMoveData(inSourceStr, outDestStr, charsToCopy);
  194.  
  195.     return outDestStr;
  196. }
  197.  
  198. #endif